Coding Conventions
Welcome to coding guidelines for AOK Navida. These guidelines are designed to ensure consistent practices, maintainable code, and a high-quality user experience in our project.
Table of Contents
1. Swift Language
1.1 Naming Conventions
- Follow Swift's naming conventions for variables, constants, functions, types, and protocols.
- Use descriptive names that convey the purpose and meaning of the entity.
1.2 Code Structure
- Maintain consistent indentation using 4 spaces (no tabs).
- Keep lines to a maximum of 100 characters.
- Use vertical whitespace to separate logical sections of code.
- Organize code into logical units and use extensions to group related functionality.
1.3 Error Handling
- Use Swift's native error handling mechanism using
try
,catch
, andthrow
. - Prefer specific error types over generic
Error
when possible to provide meaningful error information.
1.4 Performance
- Optimize performance by minimizing unnecessary memory allocations and operations.
- Leverage value types (structs, enums) to benefit from Swift's copy-on-write behavior.
2. SwiftUI Framework
2.1 View Structure
- Organize SwiftUI views hierarchically for reusability and readability.
- Break complex UI into smaller, focused subviews using the Single Responsibility Principle.
2.2 Modifiers and Styling
- Chain SwiftUI modifiers with each modifier on its own line, indented.
- Use custom styles for consistent visual attributes like fonts, colors, and spacing.
2.3 State and Data
- Use appropriate property wrappers for managing state:
@State
,@Binding
,@ObservedObject
, etc. - Follow the MVVM pattern for separating data management from view presentation.
2.4 Navigation
- Use
NavigationView
for hierarchical navigation flow. - Employ
NavigationLink
for in-app navigation within aNavigationView
.
2.5 Accessibility
- Prioritize accessibility by providing labels, hints, and traits to your SwiftUI views.
- Use SwiftUI's accessibility modifiers for enhanced usability.
2.6 Testing
- Write unit tests for your SwiftUI views and view models to ensure correctness.
- Utilize snapshot testing libraries to verify UI appearance.
References
The following documents are used as base for this set of rules.
- Apple Swift API Design Guidelines
- Raywenderlich Swift Guidelines
- LinkedIn Swift Style Guide
- Github Swift Style Guide
These combined Swift and SwiftUI coding guidelines aim to help us create maintainable, accessible, and consistent apps. By following these practices, we can ensure a high-quality user experience and a well-organized codebase.
Feel free to customize these guidelines to align with our project's specific needs while keeping the principles of readability and maintainability in mind.
Happy coding!